<template>
  <div class="register-container">
    <div class="register-form-container">
      <div class="register-title">
        <h2>积分商城</h2>
        <p class="register-subtitle">用户注册</p>
      </div>

      <van-form @submit="onSubmit">
        <van-cell-group>
          <van-field
            v-model="username"
            name="username"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请输入用户名' }]"
            left-icon="user-o"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            placeholder="请输入密码"
            :rules="[
              { required: true, message: '请输入密码' },
              { pattern: /^.{6,}$/, message: '密码长度不能少于6位' }
            ]"
            left-icon="lock"
          />
          <van-field
            v-model="confirmPassword"
            type="password"
            name="confirmPassword"
            placeholder="请确认密码"
            :rules="[
              { required: true, message: '请确认密码' },
              { validator: validateConfirmPassword, message: '两次密码输入不一致' }
            ]"
            left-icon="lock"
          />
        </van-cell-group>

        <div class="register-button">
          <van-button round block type="primary" native-type="submit" :loading="loading">注册</van-button>
        </div>
        <div class="login-link">
          <span @click="goToLogin">已有账号？立即登录</span>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import { register } from '@/api/user'

const router = useRouter()

const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const loading = ref(false)

// 验证确认密码
const validateConfirmPassword = (val: string) => {
  return val === password.value
}

// 注册表单提交
const onSubmit = async () => {
  loading.value = true

  try {
    const res = await register({
      username: username.value,
      password: password.value,
      confirmPassword: confirmPassword.value,
    })

    if (res.code === 200) {
      showSuccessToast('注册成功')
      // 跳转到登录页面
      router.replace('/login')
    } else {
      showFailToast(res.message || '注册失败')
    }
  } catch (error: any) {
    console.error('注册异常:', error)
    showFailToast(error.message || '注册异常，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped lang="scss">
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.register-form-container {
  width: 400px;
  padding: 40px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  margin-bottom: 30px;

  h2 {
    font-size: 24px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 600;
  }

  .register-subtitle {
    font-size: 14px;
    color: #999;
  }
}

:deep(.van-cell) {
  margin-bottom: 20px;
  padding: 14px 16px;
  background-color: #f8f8f8;
  border-radius: 4px;
}

:deep(.van-field__left-icon) {
  margin-right: 10px;
  color: #999;
}

.register-button {
  margin-top: 30px;

  .van-button {
    height: 44px;
    font-size: 16px;

    &:active {
      opacity: 0.8;
    }
  }
}

.login-link {
  margin-top: 16px;
  text-align: center;
  font-size: 14px;
  color: #1989fa;
}
</style> 